<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BOperationFlow
 *
 * @author I062261
 */
class BOperationFlow extends BNode {

    protected $operations = array();
    protected $currentOperation = null;
    protected $currentApp = null;

    public function getCurrentApp($data = NULL) {
        if(!$this->currentApp instanceof BApp)
            $this->currentApp = Blyn::app ()->getActiveApp ();
        return $this->currentApp;
    }
    
    public function setCurrentApp($app){
        $this->currentApp = $app;
    }

    public function getCurrentOperation($data = NULL) {
        return $this->currentOperation;
    }

    public function setCurrentOperation($operation) {
        $this->currentOperation = $operation;
    }

    public function addOperation($operation, $index = null) {

        if (!$operation instanceof BOperation)
            $operation = Blyn::app()->getActiveApp()->getOperation($operation);

        if ($operation instanceof BOperation) {
            if (is_numeric($index) && $index > 0)
                $this->operations[$index] = $operation;
            else {
                array_push($this->operations, $operation);
            }

            //return BRelationshipEntity::build($this, $operation)->save();
            return BPairEntity::buildPairEntity($this, $operation)->save();
        }
    }

    public function getUIManager($data = NULL) {
        
    }

    public function run($data = NULL) {
        
    }

    public function addContextVariable($name, $value, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getContextObj();

        /*
          if ($name == BContextObj::VariableName_Id)
          return $tObj->addContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowId, $value);
          if ($name == BContextObj::VariableName_Class)
          return $tObj->addContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowClass, $value);
         * 
         */

        $tObj->addContextVariable(BContextObj::Scope_OperationFlow, $name, $value);
    }

    public function getContextVariable($name, $scope = NULL, $type = NULL) {

        $tObj = Blyn::app()->getContextObj();

        /*
          if ($name == BContextObj::VariableName_Id)
          return $tObj->getContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowId);
          if ($name == BContextObj::VariableName_Class)
          return $tObj->getContextVariable(BContextObj::Scope_AppMeta, BContextObj::V_CurrentScreenFlowClass);
         * 
         */

        return $tObj->getContextVariable(BContextObj::Scope_OperationFlow, $name);
    }

    public function resetContextVariable($name = NULL) {

        $tObj = Blyn::app()->getContextObj();
        /**
         * for scope reset, only happen when id changed
         */
        if ($name == NULL) {
            $tId = $this->getContextVariable(BContextObj::Current_Id);
            $myId = $this->getId();
            if ($tId != $myId) {
                $tObj->resetContextVariable(BContextObj::Scope_Screen);
                $this->addContextVariable(BContextObj::Current_Id, $myId);
            }
        } else {
            $tObj->resetContextVariable(BContextObj::Scope_Screen, $name);
        }
    }

}

?>
